<?php
namespace Face\Controller;
class ShopcarController extends CommonController {

    /*购物车主页*/
    public function index()
    {
        $data = M('addcar')->where(['openid'=>$_SESSION['wechatopenid']])->select();
        $num = M('addcar')->where(['openid'=>$_SESSION['wechatopenid']])->count();
        $post = i('post.');
        if($num==0){
            $this->redirect('Face/Shopcar/car');
        }
        foreach ($data as $k => $v){
            $data[$k]['goodid'] = M('goods')->where(['id' => $v['goodid']])->find();
        }
        $this->assign('data',$data);
        if(IS_POST){
            echo '<pre/>';
            var_dump($post);
            die();
        }
        $this->display();
    }
    /*空购物车*/
    public function car(){
        $this->display();
    }

    /*接收加入购物车*/
    public function addcar()
    {
        $id = i('get.id');
        $data = [
            'openid'=>$_SESSION['wechatopenid'],
            'goodid'=>$id,
        ];
        if(M('addcar')->add($data)){
            $this->redirect('Face/Shopcar/index');
        }else{
            $this->redirect('Face/Shopcar/kong');
        }
    }
    /*删除购物信息*/
    public function del()
    {
        $id = i('get.id');
        if(M('addcar')->where(['id'=>$id])->delete()){
            $this->redirect('Face/Shopcar/index');
        }else{
            $this->redirect('Face/Shopcar/kong');
        }
    }
    /*非法*/
    public function kong()
    {
        $this->display();
    }
}